using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using System.Web;
using System.Web.UI.WebControls;
using Microsoft.SharePoint.WebPartPages;

namespace SPImageCarousel
{
    public class PicListToolPart : ToolPart
    {
      
        protected DropDownList picLists = new DropDownList();

        public PicListToolPart()
        {            
            this.Title = "Select a list";
        }

        protected override void RenderToolPart(System.Web.UI.HtmlTextWriter output)
        {            
            output.Write("<br/>");
            picLists.RenderControl(output);
        }

        protected override void OnPreRender(EventArgs e)
        {

            SPImageCarousel webPart = (SPImageCarousel)this.ParentToolPane.SelectedWebPart;

            //get the current lists in the site.

            SPSite site = SPContext.GetContext(HttpContext.Current).Site;
            SPWeb web = site.OpenWeb();

            picLists.Items.Clear();
            foreach (SPList list in web.Lists)
            {
                if (list.BaseTemplate == SPListTemplateType.PictureLibrary)
                {
                    ListItem item = new ListItem(list.Title, list.Title);
                    item.Selected = list.Title == webPart.ImageLibraryName;
                    picLists.Items.Add(item);
                }
            }

            base.OnPreRender(e);
        }

        protected override void CreateChildControls()
        {
            this.Controls.Add(picLists);
            base.CreateChildControls();
        }

        public override void ApplyChanges()
        {
            SPImageCarousel webPart = (SPImageCarousel)this.ParentToolPane.SelectedWebPart;
            webPart.ImageLibraryName = picLists.SelectedValue;               
            base.ApplyChanges();
            
        }

    }
}
